package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.xiaocz.common.Common;
import com.xiaocz.common.http.AsyncHttpPostFormData;
import com.xiaocz.common.http.AsyncHttpResponse;
import com.xiaocz.common.http.AsyncOkHttpClient;
import com.xiaocz.common.http.AsyncResponseHandler;
import com.xiaocz.common.model.RspModel;
import com.xiaocz.common.utils.AccountManagers;
import com.xiaocz.common.utils.SPUtil;
import com.xiaocz.hblsubstitutedriving.App;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.utils.AppUtil;
import com.xiaocz.hblsubstitutedriving.widget.ClearTextIconEditText;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Author:  xiaocz.
 * Descr:  修改密码
 */
public class UpdatePwdActivity extends BaseActivity {


    @BindView(R.id.edit_old_pwd)
    ClearTextIconEditText mEditOlodPwd;
    @BindView(R.id.edit_new_pwd)
    ClearTextIconEditText mEditNewPwd;
    @BindView(R.id.edit_confirm_pwd)
    ClearTextIconEditText mEditConfirm;

    public static void show(Context context) {
        Intent intent = new Intent(context, UpdatePwdActivity.class);
        context.startActivity(intent);
    }


    @OnClick(R.id.btn_ok)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                onCheckPwd();
                break;
        }
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        mEditOlodPwd.setMaxLength(mEditOlodPwd, 20, "旧密码");
        mEditNewPwd.setMaxLength(mEditNewPwd, 20, "新密码");
        mEditConfirm.setMaxLength(mEditConfirm, 20, "确认密码");
    }

    /**
     * 密码审核
     */
    private void onCheckPwd() {
        String valueOlodPwd = mEditOlodPwd.getText().toString().trim();
        String valueNewPwd = mEditNewPwd.getText().toString().trim();
        String valueConfirmPwd = mEditConfirm.getText().toString().trim();
        if (TextUtils.isEmpty(valueOlodPwd)) {
            App.getInstance().showMessage(getResources().getString(R.string.message_lod_pwd));
            return;
        }
        if (valueOlodPwd.length() <= 5) {
            App.getInstance().showMessage(getResources().getString(R.string.message_lod_pwd_length));
            return;
        }
        if (TextUtils.isEmpty(valueNewPwd)) {
            App.getInstance().showMessage(getResources().getString(R.string.message_new_pwd));
            return;
        }
        if (valueNewPwd.length() <= 5) {
            App.getInstance().showMessage(getResources().getString(R.string.message_new_pwd_length));
            return;
        }
        if (TextUtils.isEmpty(valueConfirmPwd)) {
            App.getInstance().showMessage(getResources().getString(R.string.message_confirm_pwd));
            return;
        }
        if (valueConfirmPwd.length() <= 5) {
            App.getInstance().showMessage(getResources().getString(R.string.message_confirm_pwd_length));
            return;
        }
        if (!valueNewPwd.equals(valueConfirmPwd)) {
            App.getInstance().showMessage(getResources().getString(R.string.message_pwd_disaffinity));
            return;
        }
        if (valueNewPwd.equals(valueOlodPwd)) {
            App.getInstance().showMessage(getResources().getString(R.string.message_pwd_xt));
        } else {
            onUpdatePwd(AccountManagers.getUserId(), valueOlodPwd, valueConfirmPwd);
        }
    }

    /**
     * 修改密码
     *
     * @param id     用户Id
     * @param pwd    旧密码
     * @param newPwd 新密码
     */
    private void onUpdatePwd(String id, String pwd, String newPwd) {
        loadingDialogShow(false);
        AsyncOkHttpClient okHttpClient = new AsyncOkHttpClient();
        AsyncHttpPostFormData asyncHttpPostFormData = new AsyncHttpPostFormData();
        asyncHttpPostFormData.addFormData("driver_id", id);
        asyncHttpPostFormData.addFormData("oldPwd", pwd);
        asyncHttpPostFormData.addFormData("newPwd", newPwd);
        okHttpClient.post(Common.Constant.API_IP + "api/Driver/driverUpdatePwdByPwd", asyncHttpPostFormData, new AsyncResponseHandler() {
            @Override
            public void onFailure(IOException e) {
                App.getInstance().showMessage(getResources().getString(R.string.connection_error));
                onDismiss();
                e.printStackTrace();
            }

            @Override
            public void onSuccess(AsyncHttpResponse response) {
                onDismiss();
                Type type = new TypeToken<RspModel<String>>() {
                }.getType();
                RspModel<String> rspModel = getAnalysis(response, type, "修改密码");
                if (rspModel == null) {
                    return;
                }
                switch (rspModel.getCode()) {
                    case 200:
                        if (rspModel.getMessage().contains("成功")) {
                            SPUtil.put(Common.Static.pwd_key, "");
                            AppUtil.finishAll();
                            finish();
                            LoginActivity.show(mContext);
                        }
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                    default:
                        App.getInstance().showMessage(rspModel.getMessage());
                        break;
                }
            }
        });
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_update_pwd;
    }
}
